﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using cmdb.keede.common;
using cmdb.keede.entity;
using cmdb.keede.service;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;

namespace cmdb.keede.com.Controllers
{
    public class BusinessController : Controller
    {
        private readonly IBusinessInfoService _businessService;
        private readonly IEnvironmentInfoService _evnService;

        public BusinessController(IBusinessInfoService businessService, IEnvironmentInfoService evnService)
        {
            _businessService = businessService;
            _evnService = evnService;
        }

        public async Task<IActionResult> Index()
        {
            var envList = await _evnService.GetAllEnvironmentInfosAsync();
            ViewBag.Environments = envList.Select(s => new SelectListItem
            {
                Value = s.Id.ToString(),
                Text = s.Name
            });

            return View();
        }

        public async Task<BizResult<List<BusinessInfo>>> GetBusinessInfoes(int pageIndex, int pageSize)
        {
            return await _businessService.GetBusinessInfosAsync(pageIndex, pageSize);
        }

        public async Task<BizResult<List<BusinessInfo>>> GetBusinessInfoesByEnvId(int environmentInfoId)
        {
            return await _businessService.GetBusinessInfosAsync(environmentInfoId);
        }

        public async Task<BizResult<BusinessInfo>> GetBusinessInfo(int id)
        {
            return await _businessService.GetBusinessInfoAsync(id);
        }

        public async Task<BizResult<bool>> DeleteBusinessInfo(int id)
        {
            return await _businessService.DeleteBusinessInfoAsync(id);
        }

        [HttpPost]
        public async Task<BizResult<bool>> AddOrUpdateBusinessInfo(BusinessInfo business)
        {
            return await _businessService.AddOrUpdateBusinessInfoAsync(business);
        }
    }
}